package com.thwj.project.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thwj.common.exception.ServiceException;
import com.thwj.common.utils.StringUtils;
import com.thwj.project.biz.domain.User;
import com.thwj.project.biz.domain.UserAuthentication;
import com.thwj.project.biz.enums.AuditStatusEnum;
import com.thwj.project.biz.enums.UserAuthenticationTypeEnum;
import com.thwj.project.biz.mapper.UserAuthenticationMapper;
import com.thwj.project.biz.service.IUserAuthenticationService;
import com.thwj.project.biz.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * 用户认证记录Service业务层处理
 *
 * @author generate
 * @date 2023-06-05
 */
@Service
public class UserAuthenticationServiceImpl extends ServiceImpl<UserAuthenticationMapper, UserAuthentication> implements IUserAuthenticationService {

    @Autowired
    ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    IUserService userService;

    @Override
    public int auditUserAuthentication(UserAuthentication current) {
        UserAuthentication authentication = this.getById(current.getId());
        // 审核前后状态没有改变 不做其他调整
        if (authentication.getStatus() == current.getStatus()) {
            return 0;
        }
        if (StringUtils.isEmpty(current.getRemark()) && AuditStatusEnum.auditing.getValue() != current.getStatus()) {
            throw new ServiceException("审核不通过，必须填写备注，备注将会通知给用户");
        }
        User user = userService.getById(authentication.getUserId());
        UserAuthenticationTypeEnum typeEnum = UserAuthenticationTypeEnum.getEnumByValue(authentication.getAuthenticationType());
        AuditStatusEnum statusEnum = AuditStatusEnum.getEnumByValue(current.getStatus());
        if (AuditStatusEnum.auditing_success.getValue() == statusEnum.getValue()) {
            userService.update(new UpdateWrapper<User>()
                    .set("authentication_type", typeEnum.getValue())
                    .set(UserAuthenticationTypeEnum.person.getValue() == authentication.getAuthenticationType(),"authentication_job_name", authentication.getJobName())
                    .set(UserAuthenticationTypeEnum.club.getValue() == authentication.getAuthenticationType(),"authentication_club_name", authentication.getClubName())
                    .eq("id", authentication.getUserId()));
        }
        this.updateById(current);

//        applicationEventPublisher.publishEvent(
//                WxMpMessageEvent.builder()
//                        .userId(authentication.getUserId())
//                        .miniAppPath("/pages/authentication/authentication")
//                        .pushTemplateEnum(PushTemplateEnum.AUTHTHENTICATION_AUDITING)
//                        .templateDataList(
//                                PushTemplateEnum.getAUTHTHENTICATION_AUDITING_Data(
//                                        typeEnum.getName(),
//                                        user.getUserNickName(),
//                                        statusEnum.getName(),
//                                        new Date())
//                        ).build()
//        );
        return 1;
    }
}
